/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commons.lang.StringUtils;

import org.apache.commons.lang.StringUtils;

/**
 *
 * @author WangShuai
 */
public class Demo1 {

    public static void main(String[] args) {
        
        //1.StringUtils.abbreviation:截取字符串并添加省略号

        //        String test = "This is a test of the abbreviation.";
        //        String test2 = "Test";
        //        String test3 = "中华人民共和国中华人民共和国";  //每个汉字也算1个单位
        //        System.out.println(StringUtils.abbreviate(test, 10));
        //        System.out.println(StringUtils.abbreviate(test2, 10));
        //        System.out.println(StringUtils.abbreviate(test3, 10));
        //
        //        String message = "There was a palpable sense that the rest of the world " +
        //                "might, one day, cease to exist. In the midst of the " +
        //                "confusion - the absence of firm ground - something would " +
        //                "fail to recover. The whole city is still, in a way, " +
        //                "holding it's breath, hoping the the worst has come and " +
        //                "gone.";
        //        int index = message.indexOf("ground");
        //        int offset = index - 20;
        //        int width = message.length() - offset;
        //        String context = StringUtils.abbreviate(message, offset, width);
        //        System.out.println("The word 'ground' in context: " + context);

        //2. StringUtils.chomp: 祛除字符串后面的回车换行

        String input = "Hello\n,I am wangshuai \n";
        String chomped = StringUtils.chomp(input);
        //System.out.println(input);
        System.out.println(chomped);

        //3.StringUtils.trim
        
        //A control character is defined as all characters below 32 on the ASCII table—everything from 0 (NUL) to 31 (unit separator).
        //StringUtils.trim() takes a string and removes every whitespace and control character from the beginning and the end
        String test1 = " \r Testing 1 2 3 ";
        String test2 = " \r\n ";
        String trimTest1 = StringUtils.trim(test1);
        String trimTest2 = StringUtils.trimToNull(test2);
        System.out.println(trimTest1);
        System.out.println(trimTest2);

        //4.StringUtils.strip
        
        //The strip( ) method differs from trim( ) in that you can specify a set of characters to strip from the beginning and end of a string.
        String original = "-------***---SHAZAM!---***-------";
        String stripped = StringUtils.strip(original, "-*");
        System.out.println("Stripped: " + stripped);

        //5.StringUtils.left  从左侧开始截取指定数目的字符串
        //If len characters are not available, or the String is null, the String will be returned without an exception. An exception is thrown if len is negative.
        System.out.println(StringUtils.left("abcde", 2));
    }
}
